<template>
  <div class="login">
    <!-- 登录页头部 -->
    <div class="login-header">
      <div class="flex-y-center">
        <el-image :src="logo" class="logo" />
        <span class="title">Huishi</span>
        <el-tag size="small" type="success">
          {{ defaultSettings.version }}
        </el-tag>
      </div>

      <div class="flex-y-center">
        <el-switch
          v-model="isDark"
          inline-prompt
          active-icon="Moon"
          inactive-icon="Sunny"
          @change="toggleTheme"
        />
        <lang-select class="ml-2 cursor-pointer" />
      </div>
    </div>

    <!-- 登录与注册内容 -->
    <div class="login-content">
      <div class="login-img">
        <el-image :src="loginImage" style="width: 210px" />
      </div>
      <el-tabs type="border-card" :stretch="true">
        <!-- 登录 -->
        <el-tab-pane label="Login">
          <div class="login-form">
            <el-form ref="loginFormRef" :model="loginData" :rules="loginRules">
              <div class="form-title">
                {{ $t("login.login") }}
              </div>

              <!-- 用户名 -->
              <el-form-item prop="email">
                <div class="input-wrapper">
                  <el-icon class="mx-2">
                    <User />
                  </el-icon>
                  <el-input
                    ref="email"
                    v-model="loginData.email"
                    :placeholder="$t('login.email')"
                    name="email"
                    size="large"
                    class="h-[48px]"
                  />
                </div>
              </el-form-item>

              <!-- 密码 -->
              <el-tooltip
                :visible="isCapslock"
                :content="$t('login.capsLock')"
                placement="right"
              >
                <el-form-item prop="password">
                  <div class="input-wrapper">
                    <el-icon class="mx-2">
                      <Lock />
                    </el-icon>
                    <el-input
                      v-model="loginData.password"
                      :placeholder="$t('login.password')"
                      type="password"
                      name="password"
                      size="large"
                      class="h-[48px] pr-2"
                      show-password
                      @keyup="checkCapslock"
                      @keyup.enter="handleLoginSubmit"
                    />
                  </div>
                </el-form-item>
              </el-tooltip>

              <!-- 验证码 -->
              <el-form-item prop="captchaCode">
                <div class="input-wrapper">
                  <svg-icon icon-class="captcha" class="mx-2" />
                  <el-input
                    v-model="loginData.captchaCode"
                    auto-complete="off"
                    size="large"
                    class="flex-1"
                    :placeholder="$t('login.captchaCode')"
                    @keyup.enter="handleLoginSubmit"
                  />

                  <el-image
                    :src="captchaBase64"
                    class="captcha-img"
                    @click="getCaptcha"
                  />
                </div>
              </el-form-item>

              <div class="flex-x-between w-full py-1">
                <el-checkbox>
                  {{ $t("login.rememberMe") }}
                </el-checkbox>

                <el-link type="primary" href="/forget-password">
                  {{ $t("login.forgetPassword") }}
                </el-link>
              </div>

              <!-- 登录按钮 -->
              <el-button
                :loading="loading"
                type="primary"
                size="large"
                class="w-full"
                @click.prevent="handleLoginSubmit"
              >
                {{ $t("login.login") }}
              </el-button>

              <!-- 第三方登录 -->
              <el-divider>
                <el-text size="small">
                  {{ $t("login.otherLoginMethods") }}
                </el-text>
              </el-divider>
              <div class="g-signin-button">
                <div
                  id="g_id_onload"
                  data-client_id="584416547054-ua6r8l5p76sooorsi7bk477b7bt2mv0a.apps.googleusercontent.com"
                  data-context="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"
                  data-ux_mode="popup"
                  data-callback="googleSignCallBack"
                  data-close_on_tap_outside="false"
                  data-itp_support="false"
                />

                <div
                  class="g_id_signin"
                  data-type="standard"
                  data-shape="pill"
                  data-theme="outline"
                  data-text="signin_with"
                  data-size="medium"
                  data-logo_alignment="center"
                  data-locale="en-US"
                  data-width="340"
                />
              </div>
            </el-form>
          </div>
        </el-tab-pane>
        <!-- 注册 -->
        <el-tab-pane label="Sign Up">
          <div class="login-form">
            <el-form
              ref="registerFormRef"
              :model="registerData"
              :rules="registerRules"
            >
              <div class="form-title">{{ $t("login.signUp") }}</div>

              <!-- 用户名 -->
              <el-form-item prop="email">
                <div class="input-wrapper">
                  <el-icon class="mx-2">
                    <User />
                  </el-icon>
                  <el-input
                    ref="email"
                    v-model="registerData.email"
                    :placeholder="$t('login.email')"
                    name="email"
                    size="large"
                    class="h-[48px]"
                  />
                </div>
              </el-form-item>

              <!-- 密码 -->
              <el-tooltip
                :visible="isCapslock"
                :content="$t('login.capsLock')"
                placement="right"
              >
                <el-form-item prop="password">
                  <div class="input-wrapper">
                    <el-icon class="mx-2">
                      <Lock />
                    </el-icon>
                    <el-input
                      v-model="registerData.password"
                      :placeholder="$t('login.passwordTips')"
                      type="password"
                      name="password"
                      size="large"
                      class="h-[48px] pr-2"
                      show-password
                      @keyup="checkCapslock"
                    />
                  </div>
                </el-form-item>
              </el-tooltip>

              <!-- 确认密码 -->
              <el-tooltip
                :visible="isCapslock"
                :content="$t('login.capsLock')"
                placement="right"
              >
                <el-form-item prop="newPassword">
                  <div class="input-wrapper">
                    <el-icon class="mx-2">
                      <Lock />
                    </el-icon>
                    <el-input
                      v-model="registerData.newPassword"
                      :placeholder="$t('login.newPassword')"
                      type="password"
                      name="password"
                      size="large"
                      class="h-[48px] pr-2"
                      show-password
                      @keyup="checkCapslock"
                    />
                  </div>
                </el-form-item>
              </el-tooltip>
              <div class="flex-x-between w-full py-1">
                <el-checkbox
                  style="word-wrap: break-word; overflow-wrap: break-word"
                >
                  I agree to the
                  <el-link type="primary" :underline="false" target="_blank">
                    Terms of Service
                  </el-link>
                  and
                  <el-link type="primary" :underline="false" target="_blank">
                    Privacy Policy
                  </el-link>
                  .
                </el-checkbox>
              </div>

              <!-- 注册按钮 -->
              <el-button
                :loading="loading"
                type="primary"
                size="large"
                class="w-full"
                @click.prevent="handleRegisterSubmit"
              >
                {{ $t("login.signUp") }}
              </el-button>

              <!-- 第三方登录 -->
              <el-divider>
                <el-text size="small">
                  {{ $t("login.otherLoginMethods") }}
                </el-text>
              </el-divider>
              <div class="g-signin-button">
                <div
                  id="g_id_onload"
                  data-client_id="584416547054-ua6r8l5p76sooorsi7bk477b7bt2mv0a.apps.googleusercontent.com"
                  data-context="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"
                  data-ux_mode="popup"
                  data-callback="googleSignCallBack"
                  data-close_on_tap_outside="false"
                  data-itp_support="false"
                />
                <div
                  class="g_id_signin"
                  data-type="standard"
                  data-shape="pill"
                  data-theme="outline"
                  data-text="signin_with"
                  data-size="medium"
                  data-logo_alignment="center"
                  data-locale="en-US"
                  data-width="340"
                />
              </div>
            </el-form>
          </div>
        </el-tab-pane>
      </el-tabs>
    </div>

    <!--  -->
    <div class="login-footer">
      <el-text size="small">
        Copyright © 2021 - 2024 youlai.tech All Rights Reserved.
        <el-link
          :underline="false"
          href="http://beian.miit.gov.cn/"
          target="_blank"
        >
          皖ICP备20006496号-2
        </el-link>
      </el-text>
    </div>
  </div>
</template>
<script setup lang="ts">
import { LocationQuery, useRoute } from "vue-router";

import AuthAPI, {
  type LoginData,
  type GoogleLoginData,
  type RegisterData,
} from "@/api/auth";
import router from "@/router";

import type { FormInstance } from "element-plus";

import defaultSettings from "@/settings";
import { ThemeEnum } from "@/enums/ThemeEnum";

import { useSettingsStore, useUserStore } from "@/store";

import CryptoJS from "crypto-js";

window.googleSignCallBack = (googleUser: GoogleLoginData) => {
  //发送后台接口验证完整性 && 登录
  userStore
    .googleLogin(googleUser)
    .then(async () => {
      // 跳转到登录前的页面
      const { path, queryParams } = parseRedirect();
      router.push({ path: path, query: queryParams });
    })
    .catch(() => {
      getCaptcha();
    })
    .finally(() => {
      loading.value = false;
    });
};

const userStore = useUserStore();
const settingsStore = useSettingsStore();

const route = useRoute();
const { t } = useI18n();
const loginFormRef = ref<FormInstance>();
const registerFormRef = ref<FormInstance>();

const isDark = ref(settingsStore.theme === ThemeEnum.DARK); // 是否暗黑模式
const loading = ref(false); // 按钮 loading 状态
const isCapslock = ref(false); // 是否大写锁定
const captchaBase64 = ref(); // 验证码图片Base64字符串

const logo = ref(new URL("../../assets/logo.png", import.meta.url).href);
const loginImage = ref(
  new URL("../../assets/images/login-image.svg", import.meta.url).href
);

/**
 * 登录相关
 */
const loginData = ref<LoginData>({
  email: "",
  password: "",
  captchaKey: "",
  captchaCode: "",
});

const loginRules = computed(() => {
  return {
    email: [
      {
        required: true,
        trigger: "blur",
        message: t("login.message.email.required"),
      },
    ],
    password: [
      {
        required: true,
        trigger: "blur",
        message: t("login.message.password.required"),
      },
      {
        min: 6,
        message: t("login.message.password.min"),
        trigger: "blur",
      },
    ],
    captchaCode: [
      {
        required: true,
        trigger: "blur",
        message: t("login.message.captchaCode.required"),
      },
      {
        min: 4,
        message: t("login.message.captchaCode.required"),
        trigger: "blur",
      },
    ],
  };
});

/** 获取验证码 */
function getCaptcha() {
  AuthAPI.getCaptcha().then((data) => {
    loginData.value.captchaKey = data.captchaKey;
    captchaBase64.value = data.captchaBase64;
  });
}

/** 登录表单提交 */
async function handleLoginSubmit() {
  loginFormRef.value?.validate((valid: boolean) => {
    if (valid) {
      loading.value = true;
      let password = loginData.value.password;
      let key = CryptoJS.enc.Utf8.parse("x8krb4xkmbonnua2");
      let encrypted = CryptoJS.AES.encrypt(password, key, {
        mode: CryptoJS.mode.ECB,
        padding: CryptoJS.pad.Pkcs7,
      });

      const tempLoginData: LoginData = {
        email: loginData.value.email,
        password: encrypted.toString(),
        captchaKey: loginData.value.captchaKey,
        captchaCode: loginData.value.captchaCode,
      };
      userStore
        .login(tempLoginData)
        .then(async () => {
          // 跳转到登录前的页面
          const { path, queryParams } = parseRedirect();
          router.push({ path: path, query: queryParams });
        })
        .catch(() => {
          getCaptcha();
        })
        .finally(() => {
          loading.value = false;
        });
    }
  });
}

/**
 * 注册相关
 */
const registerData = ref<RegisterData>({
  email: "",
  password: "",
  newPassword: "",
});

const registerRules = computed(() => {
  return {
    email: [
      {
        required: true,
        trigger: "blur",
        message: t("login.message.email.required"),
      },
    ],
    password: [
      {
        required: true,
        trigger: "blur",
        message: t("login.message.password.required"),
      },
      {
        min: 6,
        message: t("login.message.password.min"),
        trigger: "blur",
      },
    ],
    newPassword: [
      {
        required: true,
        trigger: "blur",
        message: t("login.message.password.required"),
      },
      {
        min: 6,
        message: t("login.message.password.min"),
        trigger: "blur",
      },
      {
        validator: (rule: any, value: string, callback: Function) => {
          if (value === "") {
            callback(new Error(t("login.message.newPassword.match")));
          } else if (registerData.value.password !== value) {
            callback(new Error(t("login.message.newPassword.match")));
          } else {
            callback();
          }
        },
        trigger: "blur",
      },
    ],
  };
});

/** 注册表单提交 */
async function handleRegisterSubmit() {
  registerFormRef.value?.validate((valid: boolean) => {
    if (valid) {
      loading.value = true;
      let password = registerData.value.password;
      let key = CryptoJS.enc.Utf8.parse("x8krb4xkmbonnua2");
      let encrypted = CryptoJS.AES.encrypt(password, key, {
        mode: CryptoJS.mode.ECB,
        padding: CryptoJS.pad.Pkcs7,
      });

      const tempRegisterData: RegisterData = {
        email: registerData.value.email,
        password: encrypted.toString(),
        newPassword: registerData.value.newPassword,
      };
      userStore
        .register(tempRegisterData)
        .then(async () => {
          // 跳转到登录前的页面
          const { path, queryParams } = parseRedirect();
          router.push({ path: path, query: queryParams });
        })
        .catch(() => {
          // getCaptcha();
        })
        .finally(() => {
          loading.value = false;
        });
    }
  });
}

/**
 * 解析 redirect 字符串 为 path 和 queryParams
 *
 * @returns { path: string, queryParams: Record<string, string> } 解析后的 path 和 queryParams
 */
function parseRedirect(): {
  path: string;
  queryParams: Record<string, string>;
} {
  const query: LocationQuery = route.query;
  const redirect = (query.redirect as string) ?? "/";

  const url = new URL(redirect, window.location.origin);
  const path = url.pathname;
  const queryParams: Record<string, string> = {};

  url.searchParams.forEach((value, key) => {
    queryParams[key] = value;
  });

  return { path, queryParams };
}

// 主题切换
const toggleTheme = () => {
  const newTheme =
    settingsStore.theme === ThemeEnum.DARK ? ThemeEnum.LIGHT : ThemeEnum.DARK;
  settingsStore.changeTheme(newTheme);
};

// 检查输入大小写
function checkCapslock(event: KeyboardEvent) {
  // 防止浏览器密码自动填充时报错
  if (event instanceof KeyboardEvent) {
    // isCapslock.value = event.getModifierState("CapsLock");
  }
}
const googleLoginScriptLoaded = ref(false);
onMounted(() => {
  getCaptcha();
  //只有在onMounted方法里动态加载谷歌js才会生效
  if (!googleLoginScriptLoaded.value) {
    const script = document.createElement("script");
    script.src = "https://accounts.google.com/gsi/client";
    script.onload = () => {
      googleLoginScriptLoaded.value = true;
    };
    document.head.appendChild(script);
  }
});
</script>

<style lang="scss" scoped>
.login {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  overflow-y: auto;
  background: url("@/assets/images/login-background-light.jpg") no-repeat center
    right;

  .login-header {
    position: absolute;
    top: 0;
    display: flex;
    justify-content: space-between;
    width: 100%;
    padding: 15px;

    .logo {
      width: 26px;
      height: 26px;
    }

    .title {
      margin: auto 5px;
      font-size: 26px;
      font-weight: bold;
      color: transparent;
      background: linear-gradient(to right, #3b82f6, #14b8a6);
      background-clip: text;
      -webkit-text-fill-color: transparent;
    }
  }

  .login-content {
    display: flex;
    width: 850px;
    overflow: hidden;
    background-color: #fff;
    border-radius: 5px;
    box-shadow: var(--el-box-shadow-light);

    @media (width <= 768px) {
      flex-direction: column;
      max-width: 100%;
      height: 100vh;
      border-radius: 0;
      box-shadow: none;
    }

    .login-img {
      display: flex;
      flex: 3;
      align-items: center;
      justify-content: center;
      background: linear-gradient(60deg, #165dff, #6aa1ff);

      @media (width <= 768px) {
        display: none;
      }
    }

    .login-form {
      display: flex;
      flex: 2;
      flex-direction: column;
      justify-content: center;
      min-width: 400px;
      padding: 30px;

      @media (width <= 768px) {
        width: 100%;
        padding: 0 20px;
      }

      .form-title {
        position: relative;
        display: flex;
        align-items: center;
        justify-content: center;
        padding: 20px 0;
        font-size: 20px;
        text-align: center;
      }

      .input-wrapper {
        display: flex;
        align-items: center;
        width: 100%;
      }

      .captcha-img {
        height: 48px;
        cursor: pointer;
        border-top-right-radius: 6px;
        border-bottom-right-radius: 6px;
      }

      .third-party-login {
        display: flex;
        justify-content: center;
        width: 100%;
        color: var(--el-text-color-secondary);

        *:not(:first-child) {
          margin-left: 20px;
        }

        .icon {
          cursor: pointer;
        }
      }
    }
  }

  .login-footer {
    position: absolute;
    bottom: 0;
    width: 100%;
    text-align: center;
  }
}

:deep(.el-form-item) {
  background: var(--el-input-bg-color);
  border: 1px solid var(--el-border-color);
  border-radius: 5px;
}

:deep(.el-input) {
  .el-input__wrapper {
    padding: 0;
    background-color: transparent;
    box-shadow: none;

    &.is-focus,
    &:hover {
      box-shadow: none !important;
    }

    input:-webkit-autofill {
      /* 通过延时渲染背景色变相去除背景颜色 */
      transition: background-color 1000s ease-in-out 0s;
    }
  }
}

html.dark {
  .login {
    background: url("@/assets/images/login-background-dark.jpg") no-repeat
      center right;

    .login-content {
      background: transparent;
      box-shadow: var(--el-box-shadow);
    }
  }
}
</style>
